package com.ywgt.handler;

import com.ywgt.response.Result;
import com.ywgt.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 其他异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
      log.error(e.getMessage());
      return Result.error();
    }

    /**
     * 算数异常
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
        log.error(e.getMessage());
        return Result.error().code(ResultCode.ARITHMETIC_EXCEPTION.getResultCode()).message(ResultCode.ARITHMETIC_EXCEPTION.getResultMessage());
    }

    /**
     * 其他可捕获异常
     * @param e
     * @return
     */
    @ExceptionHandler
    @ResponseBody
    public Result error(BusinessException e){
        log.error(e.getErrorMessage());
        return Result.error().message(e.getErrorMessage()).code(e.getErrorCode());
    }

}
